@extends('layouts.layouts')
@section('title')
    <title>后台首页</title>
@endsection
@section('content')
    <div class="content">
        <div class="header">
            <ul class="breadcrumb">
                <li><a href="/train/course">组合课程列表</a></li>
                <li class="active">订单列表</li>
            </ul>
        </div>
        <div class="main-content">
            <div class="btn-toolbar list-toolbar">
                <form class="form-inline" action="" method="get">
                    <div class="form-group">
                        <input type="text" class="form-control" name="name" placeholder="用户" value="{{$name}}">

                    </div>
                    @if(!$id)
                        <div class="form-group">
                            <input type="text" class="form-control" name="author" placeholder="导师" value="{{$author}}">
                        </div>
                    @endif
                    <input type="hidden" class="form-control" name="id" placeholder="{{$id}}" value="{{$id}}">
                    <button type="submit" class="btn btn-default">筛选</button>

                    <div class="form-group">
                        <span style="color:blue;">&nbsp;&nbsp;&nbsp;&nbsp;支付成功总数：{{$total}}人</span>
                    </div>
                    <div class="form-group">
                        <select name="buyDate" id="buyDate">
                            <option value="">导出选择</option>
                            <option value="ten">十天</option>
                            <option value="thirty">三十天</option>
                            <option value="sixty">两个月</option>
                            <option value="all">全部</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <select name="pay" id="pay">
                            <option value="">是否支付</option>
                            <option value="payed">已支付</option>
                            <option value="no">未支付</option>
                            <option value="all">全部</option>
                        </select>
                    </div>
                    <a onclick="exportBuyLog();" class="btn btn-success">导出</a>
                </form>
            </div>
            <div class="row">
                <div class="col-sm-12 col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-heading no-collapse">流水列表</div>
                        <table class="table table-bordered table-striped">
                            <thead>
                            <tr>
                                <th>编号</th>
                                <th>付费者</th>
                                <th>导师</th>
                                <th>期数</th>
                                <th>用户手机号</th>
                                <th>支付方式</th>
                                <th>是否支付</th>
                                <th>付款时间</th>
                                <th>金额</th>
                                @if(!$id)
                                    <th>项目</th>
                                @endif
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($orderGroups as $k => $group)
                                <?php
                                $user = App\User::where('id',$group->user_id)->select('name','mobile',"nickname")->first();
                                ?>
                                <tr>
                                    <td>{{$group->id}}</td>
                                    <td>{{$user?$user->nickname:''}}</td>
                                    <td>{{$group->courseGroup->author->name}}</td>
                                    @if(empty($group->stage))
                                        @if(strtotime('2019-03-25 23:59:59') > strtotime($group->created_at))
                                            @if(strtotime($group->created_at) > strtotime('2019-03-23 23:59:59'))
                                                @if($group->price == 599)
                                                    <td>一期</td>
                                                @else
                                                    <td>二期</td>
                                                @endif
                                            @else
                                                <td>一期</td>
                                            @endif
                                        @else
                                            <td>一期</td>
                                        @endif
                                    @else
                                        <td>{{$dateArr[$group->stage]}}期</td>
                                    @endif
                                    <td>{{$user?$user->mobile:''}}</td>
                                    <td>{{\App\Contant\FinanceRecord::trans($group->payfrom)}}</td>
                                    <td>{{$group->state==1?'支付':'未支付'}}</td>
                                    <td>{{date('Y-m-d H:i:s',strtotime($group->created_at))}}</td>
                                    <td>{{$group->price}}</td>
                                    @if(!$id)
                                        <td>课程:{{$group->courseGroup->title}}</td>
                                    @endif
                                    <td><button data-page="1" data-id="{{$group->user_id}}" class="btn btn-success btn-xs" onclick="userReserveInfo(this);">预留信息</button></td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            @if($id)
                {{$orderGroups->appends(['id'=>$id,'name'=>$name])->render()}}
            @else
                {{$orderGroups->appends(['name'=>$name,'author'=>$author])->render()}}
            @endif
        </div>
    </div>
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">预留信息</h4>
                </div>
                <div class="modal-body"></div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">

        function userReserveInfo(obj){
            var id = $(obj).attr('data-id');
            var data = {id:id};
            $.ajax({
                url:'/train/user/reserve',
                data:data,
                type:'GET',
                dataType:'json',
                success:function(res){
                    if(res.code == 0){
                        $('#myModal .modal-body').html(res.body);
                        $('#myModal').modal('show');
                    }
                }
            })
        }

        function exportBuyLog(){
            var buyDate = $('#buyDate').val();
            var name = $("input[name='name']").val();
            var id = $("input[name='id']").val();
            var pay = $("#pay").val();
            var url = '/train/buy/export?name='+name+'&buyDate='+buyDate+'&id='+id+'&pay='+pay;
            window.location.href = url;
        }
    </script>
@endsection